﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Diagnostics;

using Patterns.Validation.Framework;

namespace Patterns.Validation.Advanced
{
	/// <summary>
	/// 	Contains a number of methods to assert that a nullable numeric value has the expected value.
	/// </summary>
	[DebuggerNonUserCode]
	public static class NullableNumericExtensions
	{
		/// <summary>
		/// 	Validates that a nullable numeric value is not <c>null</c>.
		/// </summary>
		public static AndConstraint<NumericValidator<T?>> HaveValue<T>(this NumericValidator<T?> parent) where T : struct
		{
			return HaveValue(parent, String.Empty);
		}

		/// <summary>
		/// 	Validates that a nullable numeric value is not <c>null</c>.
		/// </summary>
		/// <typeparam name = "T"></typeparam>
		/// <param name = "parent">The parent.</param>
		/// <param name = "reason">A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.</param>
		/// <param name = "reasonArgs">Zero or more objects to format using the placeholders in <see cref = "reason" />.</param>
		/// <returns></returns>
		public static AndConstraint<NumericValidator<T?>> HaveValue<T>(this NumericValidator<T?> parent, string reason,
		                                                               params object[] reasonArgs) where T : struct
		{
			Execute.Verification
				.ForCondition(!ReferenceEquals(parent.Subject, null))
				.BecauseOf(reason, reasonArgs)
				.FailWith("Expected a value{reason}.");

			return new AndConstraint<NumericValidator<T?>>(parent);
		}

		/// <summary>
		/// 	Validates that a nullable numeric value is <c>null</c>.
		/// </summary>
		public static AndConstraint<NumericValidator<T?>> NotHaveValue<T>(this NumericValidator<T?> parent) where T : struct
		{
			return NotHaveValue(parent, String.Empty);
		}

		/// <summary>
		/// 	Validates that a nullable numeric value is <c>null</c>.
		/// </summary>
		/// <typeparam name = "T"></typeparam>
		/// <param name = "parent">The parent.</param>
		/// <param name = "reason">A formatted phrase as is supported by <see cref = "string.Format(string,object[])" /> explaining why the assertion
		/// 	is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.</param>
		/// <param name = "reasonArgs">Zero or more objects to format using the placeholders in <see cref = "reason" />.</param>
		/// <returns></returns>
		public static AndConstraint<NumericValidator<T?>> NotHaveValue<T>(this NumericValidator<T?> parent, string reason,
		                                                                  params object[] reasonArgs) where T : struct
		{
			Execute.Verification
				.ForCondition(ReferenceEquals(parent.Subject, null))
				.BecauseOf(reason, reasonArgs)
				.FailWith("Did not expect a value{reason}, but found {0}.", parent.Subject);

			return new AndConstraint<NumericValidator<T?>>(parent);
		}
	}
}